package client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import Communication.Couple;
import Communication.InfosObjet;

public class EcranJeu extends JPanel implements Runnable {
	
	private final int B_WIDTH = 800;
    private final int B_HEIGHT = 600;
	private Controleur controleur;
	
    private Thread animator;
    private void loadImage() {
        
    }

    
	public EcranJeu(Controleur controleur,Clavier clavier){
		this.controleur = controleur;
		
		
		addKeyListener(clavier);
		setFocusable(true);
		
		setBackground(Color.BLACK);
	    setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
	    
	    setDoubleBuffered(true);

        loadImage();
	}
	
    @Override
    public void addNotify() {
        super.addNotify();

        animator = new Thread(this);
        animator.start();
    }
	
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        drawImages(g);
    }
    
	
    private void drawImages(Graphics g) {
    	//System.out.println("Dessin"+controleur.getObjetsAAfficher());
    	for(int i:controleur.getObjetsAAfficher()){
    		//System.out.println("Dessin:"+i);
    		InfosObjet infosObjet = controleur.getInfos().get(i);
    		//System.out.println("Dessin:"+infosObjet.position);
    		Image image = controleur.getEnsembleImage().getImage(infosObjet.idImage);
    		//System.out.println("Dessin:"+controleur.getCentre());
    		Couple position = infosObjet.position.moins(controleur.getCentre()).plus(B_HEIGHT/2,B_WIDTH/2);
    		//System.out.println("Dessin:"+position);
    		g.drawImage(image, position.y, position.x, this);
    	}
        Toolkit.getDefaultToolkit().sync();
    }
	
    @Override
    public void run() {
    	while(true){
    		repaint();
    	}
    }
    
}
